package com.ellecity06.common.utils

import android.app.Activity
import android.content.Intent
import android.provider.MediaStore
import androidx.fragment.app.Fragment
import com.ellecity06.common.engine.CustomCompressEngine
import com.ellecity06.common.engine.CustomCropFileEngine
import com.ellecity06.common.engine.PictureSelectorGlideEngine
import com.ellecity06.common.engine.PictureSelectorSandboxFileEngine
import com.ellecity06.common.ktx.hasCamera
import com.ellecity06.common.ktx.hasReadAudioPermission
import com.ellecity06.common.ktx.hasReadImagePermission
import com.ellecity06.common.ktx.hasReadVideoPermission
import com.ellecity06.common.widgets.dialog.showImageSelector
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.luck.picture.lib.style.PictureSelectorStyle


/**
 * @author ellecity06
 * @time 2024/10/28 18:05
 * @des
 */
/**
 * @param max 最多选择几张
 * @param crop 是否裁剪
 */
fun Fragment.imageSelect(
    max: Int = 1,
    crop: Boolean = false,
    onCancel: (() -> Unit)? = null,
    onResult: ((ArrayList<LocalMedia?>?) -> Unit)
){
    requireActivity().imageSelect(max,crop,onCancel,onResult)
}
/**
 * @param max 最多选择几张
 * @param crop 是否裁剪
 */
fun Activity.imageSelect(
    max: Int = 1,
    crop: Boolean = false,
    onCancel: (() -> Unit)? = null,
    onResult: ((ArrayList<LocalMedia?>?) -> Unit)
) {
    showImageSelector(onCamera = {
        hasCamera {
            PictureSelector.create(this)
                .openCamera(SelectMimeType.ofImage())
                .setCropEngine(if (crop) CustomCropFileEngine.createCropEngine() else null)
                .setSandboxFileEngine(PictureSelectorSandboxFileEngine())
                .forResult(object : OnResultCallbackListener<LocalMedia?> {
                    override fun onResult(result: ArrayList<LocalMedia?>?) {
                        onResult.invoke(result)
                    }

                    override fun onCancel() {
                        onCancel?.invoke()
                    }
                })
        }

    }, onGallery = {
        hasReadImagePermission {
            PictureSelector.create(this)
                .openGallery(SelectMimeType.ofImage())
                .setCropEngine(if (crop) CustomCropFileEngine.createCropEngine() else null)
                .setSandboxFileEngine(PictureSelectorSandboxFileEngine())
                .setMaxSelectNum(max)
                .setSelectorUIStyle(PictureSelectorStyle())
                .setImageEngine(PictureSelectorGlideEngine.createGlideEngine())
                .forResult(object : OnResultCallbackListener<LocalMedia?> {
                    override fun onResult(result: ArrayList<LocalMedia?>?) {
                        onResult.invoke(result)
                    }

                    override fun onCancel() {
                        onCancel?.invoke()
                    }
                })
        }

    })

}
fun Fragment.chooseImage(
    max: Int = 1,
    crop: Boolean = false,
    compress: Boolean = false,
    type: Int = SelectMimeType.ofImage(),
    onCancel: (() -> Unit)? = null,
    onResult: ((ArrayList<LocalMedia?>) -> Unit)
) {
    PictureSelector.create(this)
        .openGallery(type)
        .setSandboxFileEngine(PictureSelectorSandboxFileEngine())
        .setCropEngine(if (crop) CustomCropFileEngine.createCropEngine() else null)
        .setMaxSelectNum(max)
        .setSelectorUIStyle(PictureSelectorStyle())
        .setImageEngine(PictureSelectorGlideEngine.createGlideEngine())
        .forResult(object : OnResultCallbackListener<LocalMedia?> {
            override fun onResult(result: ArrayList<LocalMedia?>?) {
                if (!result.isNullOrEmpty()) {
                    onResult.invoke(result)
                } else {
                    onCancel?.invoke()
                }

            }

            override fun onCancel() {
                onCancel?.invoke()
            }
        })
}
/**
 *
 */
fun Activity.chooseImage(
    max: Int = 1,
    crop: Boolean = false,
    compress: Boolean = false,
    type: Int = SelectMimeType.ofImage(),
    onCancel: (() -> Unit)? = null,
    onResult: ((ArrayList<LocalMedia?>) -> Unit)
) {
    PictureSelector.create(this)
        .openGallery(type)
        .setSandboxFileEngine(PictureSelectorSandboxFileEngine())
        .setCropEngine(if (crop) CustomCropFileEngine.createCropEngine() else null)
        .setMaxSelectNum(max)
        .setSelectorUIStyle(PictureSelectorStyle())
        .setImageEngine(PictureSelectorGlideEngine.createGlideEngine())
        .forResult(object : OnResultCallbackListener<LocalMedia?> {
            override fun onResult(result: ArrayList<LocalMedia?>?) {
                if (!result.isNullOrEmpty()) {
                    onResult.invoke(result)
                } else {
                    onCancel?.invoke()
                }

            }

            override fun onCancel() {
                onCancel?.invoke()
            }
        })
}

fun Activity.chooseVideo(
    max: Int = 1,
    onCancel: (() -> Unit)? = null,
    onResult: ((ArrayList<LocalMedia?>?) -> Unit)
) {
    hasReadVideoPermission {
        PictureSelector.create(this)
            .openGallery(SelectMimeType.ofVideo())
            .setSandboxFileEngine(PictureSelectorSandboxFileEngine())
            .setMaxSelectNum(max)
            .setSelectorUIStyle(PictureSelectorStyle())
            .setImageEngine(PictureSelectorGlideEngine.createGlideEngine())
            .forResult(object : OnResultCallbackListener<LocalMedia?> {
                override fun onResult(result: ArrayList<LocalMedia?>?) {
                    onResult.invoke(result)
                }

                override fun onCancel() {
                    onCancel?.invoke()
                }
            })
    }
}

fun Activity.chooseAudio(
    max: Int = 1,
    onCancel: (() -> Unit)? = null,
    onResult: ((ArrayList<LocalMedia?>?) -> Unit)
) {
    hasReadAudioPermission {
        PictureSelector.create(this)
            .openGallery(SelectMimeType.ofAudio())
            .setSandboxFileEngine(PictureSelectorSandboxFileEngine())
            .setMaxSelectNum(max)
            .isDisplayCamera(true)
            .setSelectorUIStyle(PictureSelectorStyle())
            .setImageEngine(PictureSelectorGlideEngine.createGlideEngine())
            .setRecordAudioInterceptListener { fragment, requestCode ->
                val recordAudioIntent = Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION)
                if (recordAudioIntent.resolveActivity(fragment!!.requireActivity().packageManager) != null) {
                    fragment.startActivityForResult(recordAudioIntent, requestCode)
                } else {
                    ToastUtils.show("没有发现录音")
                }
            }
            .forResult(object : OnResultCallbackListener<LocalMedia?> {
                override fun onResult(result: ArrayList<LocalMedia?>?) {
                    onResult.invoke(result)
                }

                override fun onCancel() {
                    onCancel?.invoke()
                }
            })
    }
}